<template>
    <div class="login">

        <el-form :model="param" status-icon :rules="rules" ref="loginForm" label-width="60px" class="login-form">
            <h3 class="title">租车管理系统后台</h3>
            <el-form-item label="账号" prop="username">
                <el-input type="text" placeholder="请输入账号" auto-complete="off" v-model="param.username">
                    <i slot="prefix" class="el-input__icon input-icon"></i>
                </el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" placeholder="请输入密码" auto-complete="off" v-model="param.password">
                    <i slot="prefix" class="el-input__icon input-icon"></i>
                </el-input>
            </el-form-item>
            <el-form-item prop="code">
                <el-input
                        v-model="param.code"
                        auto-complete="off"
                        placeholder="验证码"
                        style="width: 63%">
                    <i slot="prefix" class="el-input__icon input-icon"></i>
                </el-input>
                <div class="login-code">
                    <img :src="codeUrl" @click="getCode" class="login-code-img"/>
                </div>
            </el-form-item>
            <el-checkbox v-model="rememberMe" style="margin:0px 0px 25px 60px;">记住密码</el-checkbox>
            <el-form-item style="width:100%;">
                <el-button
                        :loading="loading"
                        size="medium"
                        type="primary"
                        style="width:100%;"
                        @click="submitForm('loginForm')">
                    <span v-if="!loading">登 录</span>
                    <span v-else>登 录 中...</span>
                </el-button>
                <div style="float: right;" v-if="register">
                    <router-link class="link-type" :to="'/register'">立即注册</router-link>
                </div>
            </el-form-item>
        </el-form>

        <!--  底部  -->
        <div class="el-login-footer">
            <span>Copyright © 2018-2022 ruoyi.vip All Rights Reserved.</span>
        </div>
    </div>
</template>

<script>
import Vue from "vue";
import axios from "axios";
import VueAxios from "vue-axios";
Vue.use(VueAxios, axios);
export default {
    data: function() {
        return {
            loading:false,
            rememberMe:false,
            codeUrl: "",
            // 注册开关
            register: false,
            param: {
                username: 'admin',
                password: '123456',
                code:'',
            },
            rules: {
                username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
            },
        };
    },
    created() {
        this.getCode();
    },
    methods: {
        submitForm(formName) {
            var _this = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.loading = true;
                    axios.get('/captchaImage?code=' + this.param.code + '&uuid=' + this.param.uuid).then(res => {
                        if (res.data.code === 200) {
                            axios.post("/login?username=" + this.param.username + "&password=" + this.param.password).then(resp => {
                                if (resp.data.code === 200) {
                                    this.$message.success('登录成功');
                                    localStorage.setItem('token',resp.data.data);
                                    localStorage.setItem('ms_username', _this.param.username);
                                    _this.$router.push('/dashboard');
                                } else {
                                    this.$message({
                                        showClose: true,
                                        message: resp.data.msg,
                                        type: 'warning'
                                    });
                                    this.loading = false;
                                }
                            })
                        } else {
                            this.$message({
                                showClose: true,
                                message: res.data.msg,
                                type: 'warning'
                            });
                            this.loading = false;
                        }
                    })
                } else {
                    this.$message.error('请输入账号和密码');
                    console.log('error submit!!');
                    this.loading = false;
                    return false;
                }
            });
        },
      // axios
      //   .post(
      //     "/login?username=" +
      //       this.param.userName +
      //       "&password=" +
      //       this.param.password
      //   )
      //   .then(function (response) {
      //     if (response.data.code===2000) {
      //       _this.$message.success("登录成功！");
      //       localStorage.setItem('ms_username', _this.param.userName);
      //       localStorage.setItem('token',response.data.data);
      //       _this.$router.push('/dashboard');
      //     } else {
      //       _this.$message.error("用户名或者密码错误");
      //     }
      //   })
      //   .catch(function (error) {
      //     console.log(error);
      //     _this.$message.error("你错了。。。");
      //   });
      //   },

        getCode() {
            this.loading = false;
            this.$http.post('/captchaImage').then(res => {
                this.codeUrl = "data:image/gif;base64," + res.data.data.img;
                this.param.uuid = res.data.data.uuid;
            });
        },
    },
};
</script>

<style scoped>
.login {
    height: calc(100vh);
    display: flex;
    justify-content: center;
    align-items: center;
    /*height: 100%;*/
    background-image: url("../../assets/bg.jpg");
    background-size: cover;
}
.title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #707070;
}

.login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;
}
.login-form .el-input {
    height: 38px;
}

.login-form .el-input input {
    height: 38px;
}

.login-form .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
}
.login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
}
.login-code {
    width: 33%;
    height: 38px;
    float: right;
}
.login-code img {
    cursor: pointer;
    vertical-align: middle;
}
.el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
}
.login-code-img {
    height: 38px;
}
</style>
